#include <rtthread.h>
#include <rtdevice.h>
#include "offline.h"
#include "rtdef.h"
#include "rttypes.h"
#include "jiuyuan.h"

#define THREAD_PRIORITY         25
#define THREAD_TIMESLICE        10

#define LOG_TAG              "jiuyuan"
#define LOG_LVL              LOG_LVL_DBG
#include <ulog.h>

#define REFEREE_UART_NAME       "uart6"      /* 串口设备名称 */

/* 串口设备句柄 */
static rt_device_t referee_serial;
/* 初始化配置参数 */
struct serial_configure referee_config = RT_SERIAL_CONFIG_DEFAULT;  
/* 指向信号量的指针 */
static rt_sem_t referee_sem = RT_NULL;

static rt_uint8_t buffer[64];

JiuYuan_s JiuYuan;

/* 接收数据回调函数 */
static rt_err_t referee_recv(rt_device_t dev, rt_size_t size)
{
    rt_device_read(dev, -1, &buffer, size);

    if (buffer[0] == 0XAA ) {
        rt_sem_release(referee_sem);
        offline_event_time_update(OFFLINE_MINIPC_HEART);
    }
    return RT_EOK;
}

void parse_jiuyuan_data(JiuYuan_s *jiuyuan, const rt_uint8_t *data)
{
    jiuyuan->heaader = data[0];
    jiuyuan->cmd = data[1];
    jiuyuan->redblueid = data[2];
    jiuyuan->redblue_x = (data[4] << 8) | data[3];
    jiuyuan->redblue_y = (data[6] << 8) | data[5];
    jiuyuan->yellowid = data[7];
    jiuyuan->yellow_x = (data[9] << 8) | data[8];
    jiuyuan->yellow_y = (data[11] << 8) | data[10];
    jiuyuan->blackid = data[12];
    jiuyuan->black_x = (data[14] << 8) | data[13];
    jiuyuan->black_y = (data[16] << 8) | data[15];
    jiuyuan->tail = data[17];
}

static void referee_thread_entry(void *parameter)
{
    rt_err_t result;

    while (1)
    {
        result = rt_sem_take(referee_sem,RT_WAITING_FOREVER);
        if (result==RT_EOK)
        {
            if (buffer[2] == 0XC0) {parse_jiuyuan_data(&JiuYuan, buffer);}
            else {
                JiuYuan.heaader = buffer[0];
                JiuYuan.cmd = buffer[1];
                JiuYuan.redblueid = buffer[2];
                JiuYuan.redblue_x = (buffer[4] << 8) | buffer[3];
                JiuYuan.redblue_y = (buffer[6] << 8) | buffer[5];
                JiuYuan.tail = buffer[7];
            }
        }
        rt_thread_mdelay(2);
    }
}

void send_jiuyuan(rt_uint8_t data)
{
    if(data == 1)
    {
        rt_uint8_t tmp = 0xb2;
        rt_device_write(referee_serial, -1, &tmp, sizeof(rt_uint8_t));
    }
    else
    {
        rt_uint8_t tmp = 0xb1;
        rt_device_write(referee_serial, -1, &tmp, sizeof(rt_uint8_t));
    }

}


int referee_init(void)
{
    rt_err_t ret = RT_EOK;

    /* 查找串口设备 */
    referee_serial = rt_device_find(REFEREE_UART_NAME);
    if (!referee_serial)
    {
        LOG_E("find %s failed!\n", REFEREE_UART_NAME);
        return RT_ERROR;
    }

    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(referee_serial, RT_DEVICE_CTRL_CONFIG, &referee_config);

    /* 以 DMA 接收及轮询发送方式打开串口设备 */
    rt_device_open(referee_serial, RT_DEVICE_FLAG_RDWR |RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(referee_serial, referee_recv);

    /* 创建一个动态信号量，初始值是 0 */
    referee_sem = rt_sem_create("referee_sem", 0, RT_IPC_FLAG_PRIO);
    if (referee_sem == RT_NULL)
    {
        LOG_E("create dynamic semaphore failed.\n");
        return -1;
    }

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("referee", referee_thread_entry, RT_NULL, 2048, 20, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    LOG_D("referee thread init success!\n");

    offline_manage_obj offline_minipc;
    offline_minipc.event = OFFLINE_MINIPC_HEART;
    offline_minipc.offline_time = 1000;
    offline_minipc.error_level = OFFLINE_ERROR_LEVEL;
    offline_minipc.beep_times = BEEP_DISABLE;
    offline_minipc.enable = OFFLINE_ENABLE;
    offline_minipc.last_time = rt_tick_get();
    offline_minipc.online_state =STATE_OFFLINE;
    offline_event_init(offline_minipc);

    return ret;
}


INIT_APP_EXPORT(referee_init);

